package main

import (
	businessrouter "catering_sys_api/cmd/api/biz/business/router"
	staffrouter "catering_sys_api/cmd/api/biz/staff/router"
	storerouter "catering_sys_api/cmd/api/biz/store/router"
	userrouter "catering_sys_api/cmd/api/biz/user/router"
	"catering_sys_api/cmd/api/config"
	"catering_sys_api/cmd/api/middleware"
	"catering_sys_api/core/conf"
	"catering_sys_api/core/db"
	"catering_sys_api/core/logger"
	"catering_sys_api/core/tracing"
	"catering_sys_api/core/validate"
	"flag"
	"github.com/cloudwego/hertz/pkg/app/middlewares/server/recovery"
	"github.com/cloudwego/hertz/pkg/app/server"
	"github.com/cloudwego/hertz/pkg/common/hlog"
	loggerhertzzap "github.com/hertz-contrib/logger/zap"
)

var configFile = flag.String("f", "cmd/api/hertz_info.yaml", "the config file")

func Init() {

	hlog.SetLogger(loggerhertzzap.NewLogger(
		loggerhertzzap.WithCoreEnc(logger.NewLogger(config.Get().Zap).GetEncoder()),
		loggerhertzzap.WithCoreWs(logger.NewLogger(config.Get().Zap).GetWriteSyncer()),
		loggerhertzzap.WithExtraKeys([]loggerhertzzap.ExtraKey{"trace_id"}),
		loggerhertzzap.WithExtraKeyAsStr(),
	))
	hlog.SetLevel(hlog.LevelInfo)
	db.InitGorm(config.Get().Mysql)
	db.InitRedis(config.Get().Redis)
}

func main() {
	flag.Parse()
	var c config.Config

	conf.MustLoad(*configFile, &c)
	config.Set(c)
	Init()

	r := server.New(
		server.WithValidateConfig(validate.NewValidateConfig()),
		server.WithTracer(tracing.NewServerTracer()),
		server.WithHostPorts(c.System.Address),
		server.WithHandleMethodNotAllowed(true),
	)
	r.Use(recovery.Recovery(recovery.WithRecoveryHandler(middleware.Recovery))) //recovery
	r.Use(middleware.AuthSign())                                                //recovery
	//鉴权
	userrouter.GeneratedRegister(r)
	staffrouter.GeneratedRegister(r)
	storerouter.GeneratedRegister(r)
	businessrouter.GeneratedRegister(r)

	r.Spin()
}
